package com.example.mywanandroid.ui.nav.todo.addTodo;

import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.fragment.NavHostFragment;

import com.example.mywanandroid.R;
import com.example.mywanandroid.base.BaseFragment;
import com.example.mywanandroid.databinding.FragmentAddTodoBinding;
import com.example.mywanandroid.http.request.ResponModel;
import com.google.gson.Gson;

public class AddTodoFragment extends BaseFragment<FragmentAddTodoBinding, AddTodoViewModel> {
    private static final String TAG = "AddTodoFragment";
    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_add_todo;
    }

    @Override
    protected void initViewModel() {
        mViewModel = new ViewModelProvider(this).get(AddTodoViewModel.class);
    }

    @Override
    protected void bindViewModel() {
        mDataBinding.setViewModel(mViewModel);
    }

    @Override
    protected void init() {

        mViewModel.getAdddata().observe(this,data ->{
            Log.d(TAG, "init: " + new Gson().toJson(data));
            if (data != null) {
                Toast.makeText(getActivity(), "新增待办事项成功！", Toast.LENGTH_SHORT).show();
                NavHostFragment.findNavController(AddTodoFragment.this).navigateUp();
            }
        });

        mDataBinding.radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.btn1:
                    mViewModel.type.postValue(1);
                    break;
                case R.id.btn2:
                    mViewModel.type.postValue(2);
                    break;
                case R.id.btn3:
                    mViewModel.type.postValue(3);
                    break;
                case R.id.btn4:
                    mViewModel.type.postValue(4);
                    break;
            }
        });
    }
}
